const db = require('../config/db');
const Sequelize = db.sequelize;
const Op = Sequelize.Op;
const Student = Sequelize.import('../schema/student');


Student.sync({force: false});

class StudentModel {
    /**
     * 创建文章
     * @param data 创建文章的参数
     * @returns {Promise<void>}
     */
    static async create(data) {
        return await Student.create(data)
    }

    /**
     * 更新文章数据
     * @param id 文章ID
     * @param data 文章更新的属性参数
     */
    static async update(id, data) {
        return await Student.update(data, {
            where: {
                id
            },
            fields: ['name', 'school', 'education', 'mobile', 'teacher', 'status', 'password']
        });
    }

    static async mobile(mobile) {
        return await Student.findOne({
            where: {
                mobile
            }
        })
    }

    /**
     * 搜索文章
     * @param params keyword 关键字
     *
     * @returns  返回匹配文章标题的文章列表数据
     */
    static async search(params) {
        let {page = 1, keyword} = params;

        let ret = await Student.findAndCountAll({
            limit: 10,//每页10条
            offset: (page - 1) * 10,
            where: {
                title: {
                    // 模糊查询
                    [Op.like]: '%' + keyword + '%'
                },
                is_del: 0
            },
            'order': [
                ['id', 'DESC']
            ],
            attributes: {exclude: ['content']}
        });


        return {
            code: 200,
            data: ret.rows,
            meta: {
                current_page: parseInt(page),
                per_page: 10,
                count: ret.count,
                total: ret.count,
                total_pages: Math.ceil(ret.count / 10),
            }
        }
    }

    /**
     * 软删除文章（隐藏文章）
     * @param id 文章ID
     * @param data 文章ID
     */
    static async hidden(id, data) {
        return await Student.update(data, {
            where: {
                id,
            },
            fields: ['is_del']
        })
    }

    /**
     * 获取文章列表
     * @returns {Promise<*>}
     */
    static async list(params) {
        let ret = null;
        let {page = 1,schoolNotNull,pageSize=10} = params;
        pageSize = parseInt(pageSize)
        console.log('-------'+schoolNotNull)
        if(schoolNotNull){
            ret = await Student.findAndCountAll({
                limit: pageSize,//每页10条
                offset: (page - 1) * pageSize,
                order: [
                    ['created_at', 'DESC']
                ],
                where:{
                    school: {
                        [Op.ne]: null
                    },
                    is_del: null
                }
            });
        }else{
            ret = await Student.findAndCountAll({
                limit: pageSize,//每页10条
                offset: (page - 1) * pageSize,
                order: [
                    ['created_at', 'DESC']
                ],
                where:{
                    is_del: null
                }
            });
        }

        return {
            code: 200,
            data: ret.rows,
            meta: {
                current_page: parseInt(page),
                per_page: pageSize,
                count: ret.count,
                total: ret.count,
                total_pages: Math.ceil(ret.count / pageSize),
            }
        }
    }
}

module.exports = StudentModel
